import PreferencesUtil from '../common/utils/PreferencesUtil'
import axios from '@ohos/axios'
//2.设备管理
//2.1添加设备：AddDevice(Post)
//2.2删除设备：DeleteDevice
//2.3查询设备列表：ListDevices
//2.4显示设备：ShowDevice
//2.5修改设备：UpdateDevice(PUT)
//2.6冻结设备：FreezeDevice
//2.7解冻设备：UnfreezeDevice

import { ShadowDevice,DeviceResponse, DeviceResponseAlm } from '../types/types'
class  AxiosDevice{
  //3.显示设备影子属性：ShowDeviceShadow
    ShowDeviceShadow(token:string):PromiseLike<DeviceResponse>{
    return new Promise((resolve,reject) =>{
      const project_id:string = '2dde957e408f41859cccfdb3fa2e6268';
      const device_id:string ='66126f4c2ccc1a58388021bf_JIAJ';
      const url:string = 'https://d9a517dbcf.st1.iotda-app.cn-north-4.myhuaweicloud.com'
      axios.get(
        `${url}/v5/iot/${project_id}/devices/${device_id}/shadow`,
        {
          headers: {
            'Content-Type': 'application/json',
            'X-Auth-Token':token
          },
          responseType: 'object'
        }
      ).then(res=>{
        if (res.status === 200) {
          console.log('认证成功：'+(JSON.stringify(res.data)))
          resolve(res.data)
        }else{
          reject('请求失败！')
          console.log('认证失败：'+JSON.stringify(res.status))
        }
      })
        .catch((error)=>{
          reject('请求失败！')
          console.log('设备请求失败'+JSON.stringify(error))
        })
    })
  }

  //3.显示设备影子属性：ShowDeviceShadow
  ShowDeviceShadowalm(token:string):PromiseLike<DeviceResponseAlm>{
    return new Promise((resolve,reject) =>{
      const project_id:string = '2dde957e408f41859cccfdb3fa2e6268';
      const device_id:string ='66126f4c2ccc1a58388021bf_JIAJ2';
      const url:string = 'https://d9a517dbcf.st1.iotda-app.cn-north-4.myhuaweicloud.com'
      axios.get(
        `${url}/v5/iot/${project_id}/devices/${device_id}/shadow`,
        {
          headers: {
            'Content-Type': 'application/json',
            'X-Auth-Token':token
          },
          responseType: 'object'
        }
      ).then(res=>{
        if (res.status === 200) {
          console.log('认证成功：'+(JSON.stringify(res.data)))
          resolve(res.data)
        }else{
          reject('请求失败！')
          console.log('认证失败：'+JSON.stringify(res.status))
        }
      })
        .catch((error)=>{
          reject('请求失败！')
          console.log('设备请求失败'+JSON.stringify(error))
        })
    })
  }

}
const axiosDevice = new AxiosDevice();
export default axiosDevice as AxiosDevice
